/**
 * @author: Loy-winters1224@163.com
 * @date: 2014-9-4-下午5:33:51
 */
package net.loyintean.pearlshell.task.orientedgraph;

import java.util.Collection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import net.loyintean.pearlshell.task.orientedgraph.task.TaskNode;

/**
 * 
 * @author Loy-winters1224@163.com
 * @version 2014-9-4.1.0.0
 */
public class GraphExecutor {

	/**
	 * the factory of oriented graph.
	 * 
	 */
	private TaskGraphFactory factory;

	/**
	 * execute the oriented-graph task with arguments.
	 * <p>
	 * the arguments will map to
	 * {@link net.loyintean.pearlshell.task.orientedgraph.task.TaskExecutor#execute(Object...)}
	 * 
	 * @param arguments
	 *            data which "flow through" the oriented graph.
	 */
	public void execute(Object... arguments) {

		Collection<TaskNode> tasks = factory.build(arguments);

		// TODO should the executor be a local variable, or a member variable?
		ExecutorService executor = Executors.newFixedThreadPool(tasks.size());

		for (TaskNode taskNode : tasks) {
			// the taskNode first submit may not be start first.
			executor.submit(taskNode);
		}
		executor.shutdown();
	}

	/**
	 * encapsulation.
	 * 
	 * @author Loy-winters1224@163.com
	 */
	public GraphExecutor(TaskGraphFactory f) {
		super();
		this.factory = f;
	}

}
